package com.ycu.jk16.mall.dao;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.ORDERTABLE;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.mapping.FetchType;

import com.ycu.jk16.mall.dao.provider.OrderDynaSqlProvider;
import com.ycu.jk16.mall.domain.Order;
import com.ycu.jk16.mall.domain.OrderDetail;

/**
 * 
 * @ClassName OrderDao
 * @Description TODO(OrderDao)
 * @author 倪宗龙
 * @date 2018年12月18日 下午12:37:47
 * @version V1.0
 */
public interface OrderDao {
	// 根据订单id查询单
	@Select("select * from " + ORDERTABLE + " where id=#{id}")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "shipping_id", property = "shipping", one = @One(select = "com.ycu.jk16.mall.dao.ShippingDao.selectById", fetchType = FetchType.EAGER)),
			@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)),
			@Result(column = "create_time", property = "createTime", javaType = java.util.Date.class),
			@Result(column = "end_time", property = "endTime", javaType = java.util.Date.class),
			@Result(column = "close_time", property = "closeTime", javaType = java.util.Date.class) })
	Order selectById(@Param("id") Integer id);
	
	// 根据参数查询用户总数
	@Select("select count(*) from " + ORDERTABLE)
	Integer countOrder();

	// 根据订单no查询单
	@Select("select * from " + ORDERTABLE + " where no=#{no}")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
		@Result(column = "id", property = "id", id = true),
		@Result(column = "shipping_id", property = "shipping", one = @One(select = "com.ycu.jk16.mall.dao.ShippingDao.selectById", fetchType = FetchType.EAGER)),
		@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)),
		@Result(column = "create_time", property = "createTime", javaType = java.util.Date.class),
		@Result(column = "end_time", property = "endTime", javaType = java.util.Date.class),
		@Result(column = "close_time", property = "closeTime", javaType = java.util.Date.class) })
	Order selectByNo(@Param("no") String no);

	// 新增订单
	@SelectProvider(type = OrderDynaSqlProvider.class, method = "insertOrder")
	void addOrder(@Param("order")Order order, @Param("orderDetails")List<OrderDetail> orderDetails,@Param("userId") Integer userId);

	// 带参数查询订单列表
	@SelectProvider(type = OrderDynaSqlProvider.class, method = "selectOrderWithParams")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
		@Result(column = "id", property = "id", id = true),
		@Result(column = "shipping_id", property = "shipping", one = @One(select = "com.ycu.jk16.mall.dao.ShippingDao.selectById", fetchType = FetchType.EAGER)),
		@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)),
		@Result(column = "create_time", property = "createTime", javaType = java.util.Date.class),
		@Result(column = "end_time", property = "endTime", javaType = java.util.Date.class),
		@Result(column = "close_time", property = "closeTime", javaType = java.util.Date.class) })
	List<Order> selectOrderWithParams(Map<String, Object> params);
}
